package com.cskaoyan.controller.admin_promotion_controller;

import com.cskaoyan.bean.Ad;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.promotionVO.advertisementVO.AdListVO;
import com.cskaoyan.service.promotionService.AdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *
 */
@RestController
public class AdminAdController {

    @Autowired
    AdService adService;


    @RequestMapping("admin/ad/list")
    public BaseRespVo adList(Integer page, Integer limit, String sort, String order,String name,String content) {
        AdListVO adListVO = adService.adList(page, limit, sort, order,name,content);

        return BaseRespVo.ok(adListVO);
    }
//
//    @RequestMapping("/admin/storage/create")
//    public BaseRespVo storagePic(@RequestPart("file") MultipartFile multipartFile,HttpServletRequest request) throws IOException {
//        String fileName = multipartFile.getOriginalFilename();//获得文件名
//        String name=multipartFile.getOriginalFilename();
//        String contentType = multipartFile.getContentType();//获得文件类型
//        long size = multipartFile.getSize();//文件大小
//
//        // 当文件有后缀名时
//        if (fileName.indexOf(".") >= 0) {
//            // split()中放正则表达式; 转义字符"\\."代表 "."
//            String[] fileNameSplitArray = fileName.split("\\.");
//            // 加上random戳,防止附件重名覆盖原文件
//            fileName = fileNameSplitArray[0] + UUID.randomUUID() + "." + fileNameSplitArray[1];
//        }
//        // 当文件无后缀名时(如C盘下的hosts文件就没有后缀名)
//        if (fileName.indexOf(".") < 0) {
//            // 加上random戳,防止附件重名覆盖原文件
//            fileName = fileName + UUID.randomUUID();
//        }
//        String realPath = request.getServletContext().getRealPath(fileName);
//        File file = new File("F:\\java-exercise\\"+fileName);
//        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
//        String url="http://localhost:8083/"+"pic/"+fileName;
//        Storage storage = new Storage();
//        storage.setAddTime(timestamp);
//        storage.setDeleted(false);
//        storage.setKey(fileName);
//        storage.setName(name);
//        storage.setSize((int) size);
//        storage.setType(contentType);
//        storage.setUrl(url);
//        storage.setUpdateTime(timestamp);
//        storage.setId(1);
//        multipartFile.transferTo(file);
//        return BaseRespVo.ok(storage);
//
//    }

    @RequestMapping("/admin/ad/delete")
    public BaseRespVo adDelete(@RequestBody Ad ad) {
        ad.setDeleted(true);
        int code = adService.adDelete(ad);

        if (code == 1) {
            return BaseRespVo.ok();
        } else {
            return BaseRespVo.fail();
        }
    }

    @RequestMapping("/admin/ad/update")
    public BaseRespVo adUpdate(@RequestBody Ad ad) {
        ad = adService.adUpdate(ad);

        return BaseRespVo.ok(ad);
    }
}
